<?php
/**
 * 陈杰  18323491246
 */

namespace App\Http\Controllers;

use App\Models\Group\GroupCommentModel;
use App\Models\Group\GroupCommentSubModel;
use App\Models\Group\GroupModel;
use App\Models\Group\GroupThemeModel;
use Illuminate\Http\Request;

class GroupController extends Controller
{
    public function groupList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ($request->has('address_id') and $request->address_id != '') {
                $query->where('group.address_id', $request->address_id);
            }
            if ($request->has('status') and $request->status != '') {
                $query->where('course_sub.status', $request->status);
            }
        };

        $where = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->where('group.id', '=', $request->keywords)
                    ->orWhere('group.headman', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords);
            }
        };

        $order = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'group.' . $request->order;
        }

        $data = GroupModel::where($firstWhere)->where($where)
            ->join('member', 'group.headman', '=', 'member.id')
            ->select('group.*', 'member.phone as member_phone', 'member.name as member_name')
            ->orderByDesc($order)
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function groupDel(Request $request)
    {

    }

    public function themeList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ($request->has('group_id') and $request->group_id != '') {
                $query->where('group_theme.group_id', $request->group_id);
            }
            if ($request->has('hot') and $request->hot != '') {
                $query->where('group_theme.hot', $request->hot);
            }
        };

        $where = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->where('group_theme.id', '=', $request->keywords)
                    ->orWhere('group_theme.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords);
            }
        };

        $order = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'group_theme.' . $request->order;
        }

        $data = GroupThemeModel::where($firstWhere)->where($where)
            ->join('member', 'group_theme.member_id', '=', 'member.id')
            ->join('group', 'group_theme.group_id', '=', 'group.id')
            ->select('group_theme.*', 'member.phone as member_phone', 'member.name as member_name', 'group.name as group_name')
            ->orderByDesc($order)
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function themeDel(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id < 1)
            return self::errorMsg('请输入正确的主题id');
        if (GroupThemeModel::where('id', $id)->delete())
            return self::successMsg();
        return self::errorMsg();
    }

    public function commentList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ((int)$request->input('pid') > 0 and (int)$request->input('type') > 0) {
                $query->where('group_comment.theme_id', (int)$request->input('pid'));
            }
        };
        $where      = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->orWhere('group_comment.id', '=', $request->keywords)
                    ->orWhere('group_comment.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords)
                    ->orWhere('group_comment.theme_id', '=', $request->keywords);
            }
        };
        $order      = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'group_comment.' . $request->order;
        }
        $data = GroupCommentModel::where($firstWhere)
            ->where($where)
            ->join('member', 'group_comment.member_id', '=', 'member.id')
            ->join('group_theme', 'group_comment.theme_id', '=', 'group_theme.id')
            ->select('member.name as member_name', 'member.phone as member_phone', 'group_comment.*', 'group_theme.title as theme_title')
            ->orderByDesc($order)
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function commentDel(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id < 1)
            return self::errorMsg('请输入正确的评论id');
        if (GroupCommentModel::where('id', $id)->delete())
            return self::successMsg();
        return self::errorMsg();
    }

    public function commentSubList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ((int)$request->input('comment_id') > 0) {
                $query->where('group_comment_sub.comment_id', (int)$request->input('comment_id'));
            }
        };
        $where      = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->orWhere('group_comment_sub.id', '=', $request->keywords)
                    ->orWhere('group_comment_sub.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords);
            }
        };
        $order      = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'group_comment_sub.' . $request->order;
        }
        $data = GroupCommentSubModel::where($firstWhere)
            ->where($where)
            ->join('member', 'group_comment_sub.member_id', '=', 'member.id')
//            ->join('group_theme', 'group_comment_sub.theme_id', '=', 'group_theme.id')
            ->select('member.name as member_name', 'member.phone as member_phone', 'group_comment_sub.*')//, 'group_theme.title as theme_title')
            ->orderByDesc($order)
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function commentSubDel(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id < 1)
            return self::errorMsg('请输入正确的评论id');
        if (GroupCommentSubModel::where('id', $id)->delete())
            return self::successMsg();
        return self::errorMsg();
    }


}
